/**
 * @class   wosh::network::SocketUdp
 * @brief   The class provides the interface of a generic UDP socket.
 *          UDP (user datagram protocol) is a lightweight, unreliable,
 *          datagram-oriented, connectionless protocol.
 *
 ****************************************************************************
 * @version 0.8.377 $Id: SocketUdp.h 2850 2010-08-06 18:49:18Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Network_SocketUdp_H__
 #define __WOSH_Network_SocketUdp_H__

 #include <core/Object.h>
 #include <core/LogContext.h>
 #include <core/Reflection.h>


namespace wosh {
 namespace network {

//#define _SocketUdp_DEBUG_ENABLED

class SocketUdp;

class ISocketUdpListener
 {
	public:
		/**
		 * @brief  Callback raised on (each) incoming UDP message. Blocking (non-reentrant).
		 * @param data [in] data buffer
		 * @param maxSize [in] size of the data buffer
		 * @param source [in] source of the datagram, format IP:PORT
		 * @param socket [in] source (socket) of the event
		 */
		virtual void udpIncomingDatagram( const char * data, int maxSize, const std::string& source, wosh::network::SocketUdp* socket ) = 0;

	public:
		virtual ~ISocketUdpListener() { }

}; // class def


class SocketUdp : public IReflection
 {
	WOSH_CLASSNAME(wosh::network::SocketUdp)
	WOSH_CLASS_ABSTRACT(true)

	public:
		static const int MaxPacketLength = 1400;

	public:
		/**
		 * @brief   Empty constructor, init vars.
		 */
		SocketUdp();
		/**
		 * @brief   Empty destructor
		 */
		virtual ~SocketUdp() { }

	public:
		/**
		 * @brief  Bind the UDP socket.
		 * @param timeout [in] the timeout of the operation, 0 for total asynch mode, no timeout by default
		 * @return WRET_ERR_WRONG_STATE when already binded; WRET_ERR_INTERNAL if
		 *         the binding failed (or timed-out); WRET_OK else
		 * @note   this call will start synchronously the internal thread
		 * @see    setAddressPort()
		 */
		virtual WRESULT bindSocket( unsigned long timeout = LIMIT_ULONG_MAX ) = 0;

		/**
		 * @brief  UnBind the UDP socket.
		 * @param timeout [in] the timeout of the operation, 0 for total asynch mode, no timeout by default
		 * @return WRET_ERR_WRONG_STATE when not binded; WRET_ERR_INTERNAL if
		 *         the binding failed (or timed-out); WRET_OK else
		 * @note   this call will stop synchronously the internal thread
		 * @see    bindSocket()
		 */
		virtual WRESULT unbindSocket( unsigned long timeout = LIMIT_ULONG_MAX ) = 0;

		virtual bool isBinded() const = 0;

		/**
		 * @brief  Send an UDP datagram to remote host.
		 * @param [in] destination target address:port (such as '192.168.0.255:8787')
		 * @param [in] data the block of bytes to be sent (no marshalling)
		 * @param [in] size number of bytes of 'data' argument
		 * @return number of sent bytes
		 */
		virtual int sendDatagram( const std::string& destination, const char* data, int size ) = 0;

	public:
		virtual WRESULT bindSocket( const std::string& address_port, unsigned long timeout = LIMIT_ULONG_MAX );
		virtual WRESULT bindSocket( const std::string& address, int port, unsigned long timeout = LIMIT_ULONG_MAX );


/** @name Getters
 * @{
 ******************************************************************************/
	public:

		inline const std::string& getAddress() const				{ return this->address; }
		inline int getPort() const									{ return this->port; }
		inline const std::string& getAddressPort() const 			{ return this->address_port; }

		inline wosh::network::ISocketUdpListener* getListener()		{ return this->listener; }

//@}
/** @name Setters
 * @{
 ******************************************************************************/
	public:

		virtual bool setAddressPort( const std::string& add_value, int port_value );

		virtual bool setAddressPort( const std::string& value );
 
		virtual inline void setIgnoreSelfDatagrams( bool value )		{ this->ignoreSelf = value; }

		virtual inline void setListener( wosh::network::ISocketUdpListener* udp_listener )	{ this->listener = udp_listener; }

//@}
/** @name Internal vars
 * @{
 ******************************************************************************/
	protected:
		std::string address;		///< binding ip address (such as 192.168.0.1) [supports any interface: 0.0.0.0]
		int port;					///< binding port
		std::string address_port;	///< shadow var 'address:port'

		bool ignoreSelf;

		wosh::network::ISocketUdpListener* listener;

#ifdef _SocketUdp_DEBUG_ENABLED
		mutable wosh::LogContext Log;
#endif

//@}


}; // class def

 }; // namespace network
}; // namespace wosh

#endif //__WOSH_Network_SocketUdp_H__
